/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 15, 2012  2:17:55 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : apps.promis.in.common
 * File Name : CostProjectArticleAction.java
 * TODO
 */
package apps.promis.in.common;

import java.math.BigDecimal;
import java.util.List;

import org.apache.log4j.Logger;

import promis.utility.ACTION_HANDLER;
import promis.utility.CommonService;
import promis.utility.Utilitys;
import apps.sessionFactory;
import jpa.entitys.synchro.staging.MmMatmasInb;
import jpa.entitys.promis.common.Company;
import jpa.entitys.promis.common.CostProject;
import jpa.entitys.promis.common.CostProjectArticle;
import jpa.entitys.promis.common.CostProjectArticlePK;
import jpa.service.promis.common.CostProjectArticleServices;
import jpa.service.synchro.staging.MmMatmasInbServices;

/**
 * 
 * @author Chatree Siangklom of Administrator
 * Date =Nov 13, 2013 : 2:36:22 PM 
 * CreateBy=Chatree Siangklom
 * 
 * CostProjectArticleAction
 * TODO
 */
public class CostProjectArticleAction {	
	
	private static final Logger log = Logger.getLogger(CostProjectArticleAction.class);
	
	/**
	 * 
	 * @param company is PROMIS
	 * @param project is PROMIS
	 * @param articleCode is PROMIS
	 * @return
	 * @throws Exception
	 */
	public CostProjectArticle FindByKey(String company, String project, String articleCode) throws Exception {
		
		CostProjectArticleServices costArticleServ = new CostProjectArticleServices(sessionFactory.CreateEmPromis());
		CostProjectArticlePK costProjectArticlePK = new CostProjectArticlePK();
		costProjectArticlePK.setCompany(company);
		costProjectArticlePK.setProject(project);
		costProjectArticlePK.setArticleCode(articleCode);
		
		return costArticleServ.FindByKey(costProjectArticlePK);
	}
	
	/**
	 * 
	 * @param company is PROMIS
	 * @param project is PROMIS
	 * @param articleCode is PROMIS
	 * @return
	 * @throws Exception
	 */
	public boolean ExistingChecked(String company, String project, String articleCode) throws Exception {
		return FindByKey( company, project, articleCode) != null ? true : false; 
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:35:58 PM 
	 * CreateBy=Chatree Siangklom
	 * CostProjectArticle
	 * TODO
	 * @param mmMatmasInb
	 * @return
	 * @throws Exception
	 */
	public CostProjectArticle SaveOrUpdateCostArticle(MmMatmasInb mmMatmasInb) throws Exception {
		
		try{

			CostProjectArticleServices costProjectArticleSrv = new CostProjectArticleServices(sessionFactory.CreateEmPromis());
			
			Company company = new CompanyAction().FindBySapCompany(mmMatmasInb.getSapCompanyCode());
			CostProject project = new ProjectAction().findPlantByCode(mmMatmasInb.getSapPlantCode());
			
			log.info("Company : " + company.getCompany() + " // Project : " + project.getId().getProject() + "// ArticleCode : "+ mmMatmasInb.getArticleCode());		
			CostProjectArticle model = FindByKey(company.getCompany(), project.getId().getProject() , mmMatmasInb.getArticleCode());		
			if(model == null){
				//Insert
				model = new CostProjectArticle();
				
				//Set Id
				CostProjectArticlePK costProjectArticlePK = new CostProjectArticlePK();
				costProjectArticlePK.setCompany(company.getCompany());
				costProjectArticlePK.setProject(project.getId().getProject());
				costProjectArticlePK.setArticleCode(mmMatmasInb.getArticleCode());
				model.setId(costProjectArticlePK);
								
				model.setSyncIssync("N");
				model.setStatus(mmMatmasInb.getStatus());
				model.setLeadtime(mmMatmasInb.getLeadtime());
				model.setPrFlag(mmMatmasInb.getPrFlag());
				
				model.setArticleGroup(mmMatmasInb.getArtiGrpCode());
				model.setArticleSubGroup(mmMatmasInb.getArtiSubGrpCode());
				model.setUm(mmMatmasInb.getUm());
				
				model.setSyncExternalRefKey(model.getSyncExternalRefKey());
				model.setSyncExternalRefNo(model.getSyncExternalRefNo());
				
				model.setCreatedOn(Utilitys.ConvertStringToDateTime(mmMatmasInb.getCreatedOn()));
				model.setCreatedBy(mmMatmasInb.getCreatedBy());
				
				return costProjectArticleSrv.InsertedOrUpdated(model, ACTION_HANDLER.INSERTED);
				
			}else{
				
				model.setSyncIssync("N");
				model.setStatus(mmMatmasInb.getStatus());
				model.setLeadtime(mmMatmasInb.getLeadtime());
				model.setPrFlag(mmMatmasInb.getPrFlag());
				
				model.setArticleGroup(mmMatmasInb.getArtiGrpCode());
				model.setArticleSubGroup(mmMatmasInb.getArtiSubGrpCode());
				model.setUm(mmMatmasInb.getUm());
				
				model.setSyncExternalRefKey(model.getSyncExternalRefKey());
				model.setSyncExternalRefNo(model.getSyncExternalRefNo());
				
				if(CommonService.IsNull(mmMatmasInb.getUpdatedOn()))
					model.setLastUpdOn(Utilitys.ConvertStringToDateTime(mmMatmasInb.getCreatedOn()));				
				else
					model.setLastUpdOn(Utilitys.ConvertStringToDateTime(mmMatmasInb.getUpdatedOn()));
				
				model.setLastUpdBy(CommonService.IsNullRNull(mmMatmasInb.getCreatedBy()));
				
				return costProjectArticleSrv.InsertedOrUpdated(model, ACTION_HANDLER.UPDATED);
			}			
			
		}catch(Exception e){
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
		
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:36:03 PM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 * @throws Exception
	 */
	private void Tested() throws Exception{		
		
		MmMatmasInbServices obj = new MmMatmasInbServices(sessionFactory.CreateEmSynchro());
		List<MmMatmasInb> lst =  obj.findAllIsSync("N");
		log.info("lst.size : " +lst.size());
		
		for(MmMatmasInb mmMatmasInb : lst) {

			//Company company = new CompanyAction().FindBySapCompany(mmMatmasInb.getCompanyCode());
			//CostProject project = new ProjectAction().findPlantByCode(mmMatmasInb.getPlantCode());
			
			//log.info("ExistingChecked : " + ExistingChecked(company.getCompany(), project.getId().getProject(), mmMatmasInb.getArticleCode()));
			//log.info("mmMatmasInb.getArtiGrpCode : " + mmMatmasInb.getArtiGrpCode());
			SaveOrUpdateCostArticle(mmMatmasInb);
		}
	}

//	/**
//	 * @param args
//	 */
//	public static void main(String[] args) {
//		// TODO Auto-generated method stub
//				
//		try {
//			
//			CostProjectArticleAction obj = new CostProjectArticleAction();
//			obj.Tested();
//			
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		
//	}

}
